use iced::{Color, Theme, theme};

/// Shadcn UI inspired color palette
#[derive(Debug, Clone)]
pub struct ShadcnColors {
    // Background colors
    pub background: Color,
    pub foreground: Color,
    pub card: Color,
    pub card_foreground: Color,
    pub popover: Color,
    pub popover_foreground: Color,
    
    // Primary colors
    pub primary: Color,
    pub primary_foreground: Color,
    
    // Secondary colors
    pub secondary: Color,
    pub secondary_foreground: Color,
    
    // Muted colors
    pub muted: Color,
    pub muted_foreground: Color,
    
    // Accent colors
    pub accent: Color,
    pub accent_foreground: Color,
    
    // Destructive colors
    pub destructive: Color,
    pub destructive_foreground: Color,
    
    // Border and input
    pub border: Color,
    pub input: Color,
    pub ring: Color,
    
    // Chart colors
    pub chart_1: Color,
    pub chart_2: Color,
    pub chart_3: Color,
    pub chart_4: Color,
    pub chart_5: Color,
}

impl ShadcnColors {
    /// Dark theme colors (default)
    pub fn dark() -> Self {
        Self {
            background: Color::from_rgb(0.014, 0.016, 0.024),      // hsl(222.2 84% 4.9%)
            foreground: Color::from_rgb(0.98, 0.98, 0.98),        // hsl(210 40% 98%)
            card: Color::from_rgb(0.014, 0.016, 0.024),           // hsl(222.2 84% 4.9%)
            card_foreground: Color::from_rgb(0.98, 0.98, 0.98),   // hsl(210 40% 98%)
            popover: Color::from_rgb(0.014, 0.016, 0.024),        // hsl(222.2 84% 4.9%)
            popover_foreground: Color::from_rgb(0.98, 0.98, 0.98), // hsl(210 40% 98%)
            
            primary: Color::from_rgb(0.98, 0.98, 0.98),           // hsl(210 40% 98%)
            primary_foreground: Color::from_rgb(0.086, 0.11, 0.16), // hsl(222.2 84% 4.9%)
            
            secondary: Color::from_rgb(0.102, 0.11, 0.133),       // hsl(217.2 32.6% 17.5%)
            secondary_foreground: Color::from_rgb(0.98, 0.98, 0.98), // hsl(210 40% 98%)
            
            muted: Color::from_rgb(0.102, 0.11, 0.133),           // hsl(217.2 32.6% 17.5%)
            muted_foreground: Color::from_rgb(0.639, 0.651, 0.678), // hsl(215.4 16.3% 46.9%)
            
            accent: Color::from_rgb(0.102, 0.11, 0.133),          // hsl(217.2 32.6% 17.5%)
            accent_foreground: Color::from_rgb(0.98, 0.98, 0.98), // hsl(210 40% 98%)
            
            destructive: Color::from_rgb(0.451, 0.208, 0.208),    // hsl(0 62.8% 30.6%)
            destructive_foreground: Color::from_rgb(0.98, 0.98, 0.98), // hsl(210 40% 98%)
            
            border: Color::from_rgb(0.102, 0.11, 0.133),          // hsl(217.2 32.6% 17.5%)
            input: Color::from_rgb(0.102, 0.11, 0.133),           // hsl(217.2 32.6% 17.5%)
            ring: Color::from_rgb(0.851, 0.918, 0.988),           // hsl(212.7 26.8% 83.9%)
            
            chart_1: Color::from_rgb(0.086, 0.11, 0.16),          // hsl(220 70% 50%)
            chart_2: Color::from_rgb(0.133, 0.545, 0.133),        // hsl(160 60% 45%)
            chart_3: Color::from_rgb(0.961, 0.569, 0.196),        // hsl(30 80% 55%)
            chart_4: Color::from_rgb(0.941, 0.196, 0.502),        // hsl(280 65% 60%)
            chart_5: Color::from_rgb(0.196, 0.804, 0.196),        // hsl(340 75% 55%)
        }
    }
    
    /// Light theme colors
    pub fn light() -> Self {
        Self {
            background: Color::from_rgb(1.0, 1.0, 1.0),           // hsl(0 0% 100%)
            foreground: Color::from_rgb(0.086, 0.11, 0.16),       // hsl(222.2 84% 4.9%)
            card: Color::from_rgb(1.0, 1.0, 1.0),                // hsl(0 0% 100%)
            card_foreground: Color::from_rgb(0.086, 0.11, 0.16),  // hsl(222.2 84% 4.9%)
            popover: Color::from_rgb(1.0, 1.0, 1.0),             // hsl(0 0% 100%)
            popover_foreground: Color::from_rgb(0.086, 0.11, 0.16), // hsl(222.2 84% 4.9%)
            
            primary: Color::from_rgb(0.086, 0.11, 0.16),          // hsl(222.2 84% 4.9%)
            primary_foreground: Color::from_rgb(0.98, 0.98, 0.98), // hsl(210 40% 98%)
            
            secondary: Color::from_rgb(0.961, 0.969, 0.976),      // hsl(210 40% 96%)
            secondary_foreground: Color::from_rgb(0.086, 0.11, 0.16), // hsl(222.2 84% 4.9%)
            
            muted: Color::from_rgb(0.961, 0.969, 0.976),          // hsl(210 40% 96%)
            muted_foreground: Color::from_rgb(0.427, 0.459, 0.518), // hsl(215.4 16.3% 46.9%)
            
            accent: Color::from_rgb(0.961, 0.969, 0.976),         // hsl(210 40% 96%)
            accent_foreground: Color::from_rgb(0.086, 0.11, 0.16), // hsl(222.2 84% 4.9%)
            
            destructive: Color::from_rgb(0.937, 0.314, 0.314),    // hsl(0 84.2% 60.2%)
            destructive_foreground: Color::from_rgb(0.98, 0.98, 0.98), // hsl(210 40% 98%)
            
            border: Color::from_rgb(0.898, 0.918, 0.941),         // hsl(214.3 31.8% 91.4%)
            input: Color::from_rgb(0.898, 0.918, 0.941),          // hsl(214.3 31.8% 91.4%)
            ring: Color::from_rgb(0.376, 0.447, 0.565),           // hsl(222.2 84% 4.9%)
            
            chart_1: Color::from_rgb(0.086, 0.11, 0.16),          // hsl(220 70% 50%)
            chart_2: Color::from_rgb(0.133, 0.545, 0.133),        // hsl(160 60% 45%)
            chart_3: Color::from_rgb(0.961, 0.569, 0.196),        // hsl(30 80% 55%)
            chart_4: Color::from_rgb(0.941, 0.196, 0.502),        // hsl(280 65% 60%)
            chart_5: Color::from_rgb(0.196, 0.804, 0.196),        // hsl(340 75% 55%)
        }
    }
}

/// Custom theme for CDB Studio
#[derive(Debug, Clone)]
pub struct CdbTheme {
    pub colors: ShadcnColors,
    pub is_dark: bool,
}

impl CdbTheme {
    pub fn dark() -> Self {
        Self {
            colors: ShadcnColors::dark(),
            is_dark: true,
        }
    }
    
    pub fn light() -> Self {
        Self {
            colors: ShadcnColors::light(),
            is_dark: false,
        }
    }
}

impl Default for CdbTheme {
    fn default() -> Self {
        Self::dark()
    }
}

/// Convert to Iced Theme
impl From<CdbTheme> for Theme {
    fn from(cdb_theme: CdbTheme) -> Self {
        if cdb_theme.is_dark {
            Theme::TokyoNight
        } else {
            Theme::Light
        }
    }
}
